<%--
  Created by IntelliJ IDEA.
  User: 天青烟雨
  Date: 2020/9/24
  Time: 13:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="commons/header.jsp"%>



<table id="goodsDataTable" class="table table-bordered table-striped"></table>

<div class="modal" id="addGoodsModal" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button onclick="destoryAddGoodsModal()" class="close">
                    <span>&times;</span>
                </button>
                <h3 class="modal-title">删除订单信息</h3>
            </div>
            <div class="modal-body">
                <form>
                    <input type="hidden" id="imageServerPath">
                    <div class="form-group" style="font-size: 50px">
                        <input type="radio" name="type" value="0" width="500px" height="15px" >
                        <span class="label label-danger">硬删除</span>
                        <br>
                        <input type="radio" name="type" value="1" width="500px" height="15px" >
                        <span class="label label-warning">软删除</span>
                    </div>


                </form>
            </div>
            <div class="modal-footer">
                <button onclick="changePay()" class="btn btn-success">确定</button>
                <button onclick="destoryAddGoodsModal()" class="btn btn-danger">取消</button>
            </div>
        </div>
    </div>
</div>

<%@ include file="commons/footer.jsp"%>

<script>

    var changeId;

    $('#goodsDataTable').bootstrapTable({

        url: '${pageContext.servletContext.contextPath}/payPage',
        method: 'GET',
        columns: [
            // field是返回rows中每个对象的属性名
            {title: '支付状态', field: 'trade_status', align: 'center',formatter: formatStatus},
            {title: '支付用户', field: 'name'},
            {title: '支付用户电话', field: 'phone'},
            {title: '订单编号', field: 'order_on'},
            {title: '支付描述', field: 'pay_info',formatter: formatTitle},
            {title: '支付宝交易号', field: 'trade_no'},
            {title: '支付价格', field: 'total_amount'},
            {title: '支付流水创建时间', field: 'create_time',formatter: formatTime},
            {title: '操作',formatter: addProcessBtn}
        ],
        queryParams: function (params) {

            var name = $('#name').val();  //获取查询表单name的值
            var phone = $('#phone').val();
            var status = $('#status').val();
            var begin = $('#beginRegisterDate').val(); //重置name
            var end = $('#endRegisterDate').val(); //重置name

            params.name = name;
            params.phone = phone;
            params.status = status;
            params.begin = begin;
            params.end = end;
            params.method = 'getAllPay';
            return params;
        },
        /**
         * 决定了在哪边分页：client server, 两种不同的配置决定了服务器端返回的数据格式。
         *  client分页, 服务器端返回的数据  [{}, {}, {}]
         *  server端分页，服务器端返回的数据：{rows:[{}, {}], total: 23}
         */
        sidePagination: 'server',
        pagination: true, //表示使用分页
        toolbar: '#goodsTableToolbar' , //设置工具条
        pageList: [4, 8, 12]
    })

    function formatStatus(value, row,index) {


        // return 'TRADE_SUCCESS' == value ? '<span class="label label-success">已支付</span>' : '<span class="label label-danger">不可用</span>';
        if('TRADE_FAIL' == value ) {
            return '<span class="label label-danger">不可用</span>'
        }
        if ('TRADE_SUCCESS' == value) {
            return '<span class="label label-success">已支付</span>'
        }

        // if (value == 0) {
        //     return '<span class="label label-success">已支付</span>'
        // } else if (value == 1) {
        //     return '<span class="label label-warning">不可用</span>'
        // }
    }

    function changePay() {

        var value = $("input:radio[name='type']:checked").val();
        var goodsId = $('#deleteId').val();
        console.log(goodsId);

        if (value == 1){
            if(confirm("确定要删支付id【" + changeId + "】的数据?")){
                $.ajax({
                    url: '${pageContext.servletContext.contextPath}/payPage',
                    type: 'get',
                    dataType: 'json',
                    data: {userId: changeId, method: 'updatePay'},
                    success: function(_data) {

                        if(_data.code == 1) {
                            alert("删除成功")
                            destoryAddGoodsModal()
                            $('#goodsDataTable').bootstrapTable('refresh');
                        }else {
                            alert("删除失败")
                        }

                    }
                })
            }
        }else if (value ==0){
            if(confirm("确定要删支付id【" + changeId + "】的数据?")) {

                $.ajax({
                    url: '${pageContext.servletContext.contextPath}/payPage',
                    type: 'get',
                    dataType: 'json',
                    data: {userId: changeId, method: 'deletePay'},
                    success: function(_data) {
                        //_data = {code: 1, msg: 'success'}
                        if(_data.code == 1) {

                            alert("删除成功")
                            destoryAddGoodsModal()
                            $('#goodsDataTable').bootstrapTable('refresh');

                        }else {
                            alert("删除失败")
                                }

                            }
                        })

        }
      }
    }

    // 格式化日期
    function formatTime(value) {
        return moment(value).format('YYYY-MM-DD HH:mm:ss')
    }

    function addProcessBtn(value, row) {

        var goodsId = row.id;

        console.log(goodsId)

        return '<button onclick="getPayBtn(' + goodsId + ')" class="btn btn-danger btn-xs"  value ="'+goodsId+'">删除</button>'
    }

    //当内容长度大于30的时候，多余的不显示
    function formatTitle(value) {
        var length = value.length; //获取文本长度
        if(length > 20) {
            return value.substring(0, 21) + '...';
        }
        return value;
    }

    function getPayBtn(goodsId) {

        changeId = goodsId;
        $('#addGoodsModal').modal('show');


    }

    // 隐藏模态框
    function destoryAddGoodsModal() {

        $('#addGoodsModal').modal('hide');
    }

    // 搜索
    function querySearch() {
        // 解决翻页之后, 还是停留在以前的页面问题, 使用selectPage, 意思是回到第一页
        $('#goodsDataTable').bootstrapTable('selectPage', 1);
    }

    function resetSearch() {
        $('#name').val(''); //重置name
        $('#goods').val(''); //重置性别
        $('#status').val('-1'); //重置状态
        $('#number').val('');
        $('#beginRegisterDate').val('');
        $('#endRegisterDate').val('');
        querySearch();
    }

    $('#beginRegisterDate, #endRegisterDate').datetimepicker({
        // 格式化 yyyy-mm-dd hh:ii:ss 年-月-日 时-分-秒
        format: 'yyyy-mm-dd hh:ii:ss'
    })

</script>
